using System;
using System.Xml;

namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for QueryXsltResultUtil.
	/// </summary>
	class QueryXsltResultUtil
	{

		public static bool isFieldElementTextIsTrue( XmlDocument resultDoc, string jetId ){
			XmlElement valueChangeElement = QueryXsltResultUtil.findFieldElementByJetId( resultDoc, jetId );
			string exprResult = "false";
			if( valueChangeElement != null )
			{
				exprResult = valueChangeElement.InnerText;
			}
			if ( exprResult.Equals( "true" ) )
			{
				return true;
			}
			return false;
		}
		
		public static XmlElement findFieldElementByJetId( XmlDocument resultDoc, string jetId ){
			string[] ids = FieldUtil.parseJetId( jetId );
			string elementId = ids[ 0 ];
			string attrId = ids[ 1 ];
			string filter = "/result/field[@" + JetConstant.JetId + "='" + elementId + "']";
			XmlElement root = resultDoc.DocumentElement;
			XmlNodeList fieldElementList = root.SelectNodes( filter );
			foreach( XmlElement fieldElement in fieldElementList ){
				string attr = fieldElement.GetAttribute( JetConstant.AttributeName );
				if( attr != null && attr.Equals( attrId ) ){
					return fieldElement;
				}
			}
			return null;
		}

		public static bool existTrueResult( XmlDocument resultDoc ){
			string filter = "/result/field";
			XmlElement root = resultDoc.DocumentElement;
			XmlNodeList fieldElementList = root.SelectNodes( filter );
			foreach( XmlElement fieldElement in fieldElementList ){
				string result = fieldElement.InnerText;
				if( result.Equals( "true" ) ){
					return true;
				}
			}
			return false;
		}

		public static string findFieldValue( XmlDocument resultDoc )
		{
			string filter = "/result/field";
			XmlElement root = resultDoc.DocumentElement;
			XmlNodeList fieldElementList = root.SelectNodes( filter );
			foreach( XmlElement fieldElement in fieldElementList )
			{
				return fieldElement.InnerText;
			}
			return null;
		}

	}
}
